package com.example.chapter3_3.dao;

import com.example.chapter3_3.entity.Card;
import com.example.chapter3_3.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StudentMapper {
    //column参数是两个表关联列，@One(select ="包名.类名.方法名"
    //代码过程：先查询student表的数据，把得到的结果中的“no”列数据拿出来，
    //        再传递到select指定的方法中进行第二次查询，第二次查询得到的结果，放到property指定的参数中
    @Result(property = "card",column = "no" ,one = @One(select = "com.example.chapter3_3.dao.StudentMapper.getCardByNumber"))
    @Select("select * from tb_student where name like concat('%',#{name},'%')")
    List<Student> getStuByName(String name);

    @Select("select * from card where cardNo = #{number}")
    Card getCardByNumber(String number);

    @Select({
            "<script>",
            "select * from tb_student where 1=1",
            "<if test='name != null'>",
            "and name = #{name}",
            "</if>",
            "<if test='number != null'>",
            "and no = #{number}",
            "</if>",
            "</script>"
    })
    List<Student> getStuByNameOrNo(String name);

}
